package cn.iocoder.yudao.module.erp.controller.admin.dpitem.dpitemnj;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.erp.controller.admin.dpitem.dpitemnj.vo.DpItemNjPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.dpitem.dpitemnj.vo.DpItemNjRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.dpitem.dpitemnj.vo.DpItemNjSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dpitem.DpItemNjDO;
import cn.iocoder.yudao.module.erp.service.dpitem.DpItemNjService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import java.io.IOException;
import java.util.List;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - ERP南疆口岸制单货号详情")
@RestController
@RequestMapping("/erp/dp-item-nj")
@Validated
public class DpItemNjController {

    @Resource
    private DpItemNjService dpItemNjService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP南疆口岸制单货号详情")
    // @PreAuthorize("@ss.hasPermission('erp:dp-item-nj:create')")
    public CommonResult<Long> createDpItemNj(@Valid @RequestBody DpItemNjSaveReqVO createReqVO) {
        return success(dpItemNjService.createDpItemNj(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP南疆口岸制单货号详情")
    // @PreAuthorize("@ss.hasPermission('erp:dp-item-nj:update')")
    public CommonResult<Boolean> updateDpItemNj(@Valid @RequestBody DpItemNjSaveReqVO updateReqVO) {
        dpItemNjService.updateDpItemNj(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP南疆口岸制单货号详情")
    @Parameter(name = "id", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('erp:dp-item-nj:delete')")
    public CommonResult<Boolean> deleteDpItemNj(@RequestParam("id") Long id) {
        dpItemNjService.deleteDpItemNj(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP南疆口岸制单货号详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    // @PreAuthorize("@ss.hasPermission('erp:dp-item-nj:query')")
    public CommonResult<DpItemNjRespVO> getDpItemNj(@RequestParam("id") Long id) {
        DpItemNjDO dpItemNj = dpItemNjService.getDpItemNj(id);
        return success(BeanUtils.toBean(dpItemNj, DpItemNjRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP南疆口岸制单货号详情分页")
    // @PreAuthorize("@ss.hasPermission('erp:dp-item-nj:query')")
    public CommonResult<PageResult<DpItemNjRespVO>> getDpItemNjPage(@Valid DpItemNjPageReqVO pageReqVO) {
        PageResult<DpItemNjDO> pageResult = dpItemNjService.getDpItemNjPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DpItemNjRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP南疆口岸制单货号详情 Excel")
    // @PreAuthorize("@ss.hasPermission('erp:dp-item-nj:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDpItemNjExcel(@Valid DpItemNjPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DpItemNjDO> list = dpItemNjService.getDpItemNjPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP南疆口岸制单货号详情.xls", "数据", DpItemNjRespVO.class,
                        BeanUtils.toBean(list, DpItemNjRespVO.class));
    }

}